package com.example.controlleradvice.globalDataPreprocessor;

import org.springframework.format.datetime.DateFormatter;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.InitBinder;

/**
 * 全局数据预处理功能测试
 *
 * @author: 李明杰
 * @date: 2022/6/6 12:25
 */
@ControllerAdvice
public class GlobalConfigDataPreprocessor {

    /**
     * 添加自定义类型转换器
     * <br> @InitBinder("b")去处理controller中@ModelAttribute("b")对应的参数
     *
     * @param binder
     */
    @InitBinder("b")
    public void b(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("b.");
        binder.addCustomFormatter(new DateFormatter());
    }

    @InitBinder("a")
    public void a(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("a.");
    }

}
